package com.doctcloud.hisinfo.api.config;

import feign.codec.Decoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.nio.charset.Charset;
import java.util.Arrays;

@Configuration
public class FeignConfig {

    /**
     * 配置 Feign 使用 GBK 编码解码响应体
     * @return Decoder
     */
    @Bean
    public Decoder feignDecoder() {
        // 创建指定字符集的消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setDefaultCharset(Charset.forName("GBK"));

        // 封装为 HttpMessageConverters 的 ObjectFactory
        ObjectFactory<HttpMessageConverters> messageConverters = () ->
                new HttpMessageConverters(Arrays.asList(converter));

        // 传入 ObjectFactory 构建 SpringDecoder
        return new SpringDecoder(messageConverters);
    }
}